package com.state.impl;

import java.util.Random;
import com.state.GumballMachine;
import com.state.face.State;

/**
 * 有25美分的状态
 * 
 * @author Administrator
 * 
 */
public class HasQuarterState implements State {
	Random randomWinner = new Random(System.currentTimeMillis());
	GumballMachine gumballMachine;

	/**
	 * 通过构造器得到糖果机的引用，然后将它记录在实例变量中
	 * 
	 * @param gumballMachine
	 */
	public HasQuarterState(GumballMachine gumballMachine) {
		this.gumballMachine = gumballMachine;
	}

	public void insertQuarter() {
		System.out.println("你无须再投入另外25美分");
	}

	public void ejectQuarter() {
		System.out.println("退回25美分");
		gumballMachine.setState(gumballMachine.getNoQuarterState());
	}

	public boolean turnCrank() {
		System.out.println("转动曲柄了");
		int winner = randomWinner.nextInt(10);
		if ((winner == 0) && (gumballMachine.getCount() > 1)) {
			gumballMachine.setState(gumballMachine.getWinnerState());
		} else {
			gumballMachine.setState(gumballMachine.getSoldState());
			return false;
		}
		return true;
	}

	public void dispense() {
		System.out.println("无法出糖果");
	}

	public String toString() {
		return "等待转动曲柄";
	}
}
